package code

import (
	"fmt"
	"io"
	"log"
	"os"
	"strings"
)

// 读取文件
func readFileBuff01() {
	file, err := os.Open(`D:\myworkspace\gitee\faith-algorithm-flow\learn\gocode\base01\03io\code\1-Reader.go`)
	if err != nil {
		fmt.Println("open file err:", err)
		return
	}
	defer file.Close()

	buff := make([]byte, 512)
	for {
		n, err := file.Read(buff)
		if err != nil {
			if err == io.EOF {
				fmt.Println("read file end")
				break
			}
			log.Fatal(err)
		}
		fmt.Println("读取内容是:", string(buff[:n]))
	}
}

func readFile01() {
	reader := strings.NewReader(`hello world 是一个世界`)
	num := 0
	all := make([]byte, 0)
	for {
		buff := make([]byte, 4)
		n, err := reader.Read(buff)
		if err != nil {
			if err == io.EOF {
				fmt.Println("read file end")
				break
			}
			log.Fatal(err)
		}
		num += n
		all = append(all, buff[:n]...)
	}
	fmt.Printf("all: %v\n", string(all[:num]))
}

func Reader01() {
	readFile01()
}
